<?php
session_start();
include 'db.php'; //initialize database

// check if its incoming new customer
if($_POST['checktype'] == 'new'){	 
	// acquire values from the form
	$cName = $_POST['nName'];
	$cJobTitle = $_POST['nJobTitle'];
	$cLocation = $_POST['nLocation'];
	$cPO = $_POST['nPO'];
	$cEmail = $_POST['nEmail'];
	$cNumb = $_POST['nNumb'];
	$cCompany = $_POST['nCompany'];
	$cMobile = $_POST['nMobile'];
	// check if its a duplicate entry
	$result = mysql_query("select count(*) from customers where cNumber = '$cNumb'");
	$row = mysql_fetch_row($result);	
    $numRows = $row[0];
	echo 'NUMROWS : '.$numRows;
	if($numRows == 0){
		// let's insert variables for ultimate satisfaction
		$iyot = "INSERT INTO customers(cName, cJobTitle, cLocation, cPO, cEmail, cNumber, cCompName, cMobile, cBuyCount) values('$cName', '$cJobTitle', '$cLocation', '$cPO', '$cEmail', '$cNumb', '$cCompany', '$cMobile', 1)";
		@mysql_query($iyot);
		$newid = mysql_insert_id();
		session_start();
		//store all variable into session
		$_SESSION['customerType'] = 'new';
		$_SESSION['customerID'] = $newid;
		$_SESSION['name'] = $cName;
		$_SESSION['jobtitle'] = $cJobTitle;	
		$_SESSION['location'] = $cLocation;
		$_SESSION['PO'] = $cPO;	
		$_SESSION['email'] = $cEmail;
		$_SESSION['number'] = $cNumb;
		$_SESSION['company'] = $cCompany;
		$_SESSION['mobile'] = $cMobile;
		$_SESSION['buycount'] = 1;
		//generate proforma ID
		$salesman = $_SESSION['salesName'];
		$customName = explode(" ", $salesman);
		$proformaID = "";
		foreach ($customName as $w) {
  			$proformaID .= $w[0];
		}
		$proformaID .= date('ym');
		$countSale = mysql_query("SELECT * FROM receipts where salesman = '$salesman'"); 
		$cntSale = mysql_num_rows($countSale); 		
		$proformaID .= sprintf("%04s", ++$cntSale);
 
		//$proformaID .= sprintf("%04s", 1);
		$_SESSION['proformaID'] = $proformaID;
		header("Location: print.php");
	}else{
		 header("Location: identifyCustomer.php?action=regExist");	
		//echo 'ehut?';	
	}
}// end if checktype == new

//check if its existing customer
if($_POST['checktype'] == 'existing'){
	// acquire values from the form
	$cNumb = $_POST['eNumb'];
	// check if its a duplicate entry
	$result = mysql_query("select count(*) from customers where cNumber = '$cNumb'");
	$row = mysql_fetch_row($result);	
    $numRows = $row[0];
	if($numRows == 0){
		header("Location: identifyCustomer.php?action=regNotExist");	
	}else{	
		// let's get that customer
		$result = mysql_query("select * from customers where cNumber = '$cNumb'");
		$customer = mysql_fetch_array($result);
		//session_start();
		//store all variable into session
		$_SESSION['customerType'] = 'returning';
		$_SESSION['cID'] = $customer['customerID'];
		$_SESSION['name'] = $customer['cName'];
		$_SESSION['jobtitle'] = $customer['cJobTitle'];	
		$_SESSION['location'] = $customer['cLocation'];
		$_SESSION['PO'] = $customer['cPO'];	
		$_SESSION['email'] = $customer['cEmail'];
		$_SESSION['number'] = $customer['cNumber'];
		$_SESSION['company'] = $customer['cCompName'];
		$_SESSION['mobile'] = $customer['cMobile'];
		$_SESSION['buycount'] = $customer['cBuyCount'] + 1;
		//generate proforma ID
		$salesman = $_SESSION['salesName'];
		$customName = explode(" ", $salesman);
		$proformaID = "";
		foreach ($customName as $w) {
  			$proformaID .= $w[0];
		}
		$proformaID .= date('ym');
		$countSale = mysql_query("SELECT * FROM receipts where salesman = '$salesman'"); 
		$cntSale = mysql_num_rows($countSale); 		
		$proformaID .= sprintf("%04s", ++$cntSale);
		$_SESSION['proformaID'] = $proformaID;
		
		//go print it
		header("Location: print.php");
	}
	
}// end if: checktype == existing

?>